<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'adv', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "advinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('adv');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanEdit()) {
	$Security->SaveLastUrl();
	Page_Terminate("advlist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$adv->Export = @$_GET["export"]; // Get export parameter
$sExport = $adv->Export; // Get export parameter, used in header
$sExportFile = $adv->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["adv_id"] <> "") {
	$adv->adv_id->setQueryStringValue($_GET["adv_id"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$adv->CurrentAction = $_POST["a_edit"]; // Get action code
	GetUploadFiles(); // Get upload files
	LoadFormValues(); // Get form values
} else {
	$adv->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($adv->adv_id->CurrentValue == "") Page_Terminate($adv->getReturnUrl()); // Invalid key, exit
switch ($adv->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($adv->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$adv->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($adv->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$adv->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_adv_title"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Title"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_adv_img"];
		aelm = fobj.elements["a" + infix + "_adv_img"];
		var chk_adv_img = (aelm && aelm[0])?(aelm[2].checked):true;
		if (elm && chk_adv_img && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Image"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_adv_img"];
		if (elm && !ew_CheckFileType(elm.value)) { 
			if (!ew_OnError(elm, "File type is not allowed.")) 
				return false; 
		}
		elm = fobj.elements["x" + infix + "_adv_area"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Area"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_adv_dateadd"];
		if (elm && !ew_CheckEuroDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = dd/mm/yyyy - Date Add"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_adv_order"];
		if (elm && !ew_CheckInteger(elm.value)) {
			if (!ew_OnError(elm, "Incorrect integer - Order"))
				return false; 
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: adv<br><br><a href="<?php echo $adv->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="fadvedit" id="fadvedit" action="advedit.php" method="post" enctype="multipart/form-data" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">Advertisement ID</td>
		<td<?php echo $adv->adv_id->CellAttributes() ?>><span id="cb_x_adv_id">
<div<?php echo $adv->adv_id->ViewAttributes() ?>><?php echo $adv->adv_id->EditValue ?></div>
<input type="hidden" name="x_adv_id" id="x_adv_id" value="<?php echo ew_HtmlEncode($adv->adv_id->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Title<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $adv->adv_title->CellAttributes() ?>><span id="cb_x_adv_title">
<input type="text" name="x_adv_title" id="x_adv_title"  size="30" maxlength="250" value="<?php echo $adv->adv_title->EditValue ?>"<?php echo $adv->adv_title->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Link</td>
		<td<?php echo $adv->adv_link->CellAttributes() ?>><span id="cb_x_adv_link">
<input type="text" name="x_adv_link" id="x_adv_link"  size="30" maxlength="250" value="<?php echo $adv->adv_link->EditValue ?>"<?php echo $adv->adv_link->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Image<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $adv->adv_img->CellAttributes() ?>><span id="cb_x_adv_img">
<?php if (!is_null($adv->adv_img->Upload->DbValue)) { ?>
<input type="radio" name="a_adv_img" id="a_adv_img" value="1" checked>Keep&nbsp;
<input type="radio" name="a_adv_img" id="a_adv_img" value="2" disabled>Remove&nbsp;
<input type="radio" name="a_adv_img" id="a_adv_img" value="3">Replace<br>
<?php } else { ?>
<input type="hidden" name="a_adv_img" id="a_adv_img" value="3">
<?php } ?>
<input type="file" name="x_adv_img" id="x_adv_img"  size="30" onchange="if (this.form.a_adv_img[2]) this.form.a_adv_img[2].checked=true;"<?php echo $adv->adv_img->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Area<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $adv->adv_area->CellAttributes() ?>><span id="cb_x_adv_area">
<?php
$arwrk = $adv->adv_area->EditValue;
if (is_array($arwrk)) {
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($adv->adv_area->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " checked" : "";
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="radio" name="x_adv_area" id="x_adv_area" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $adv->adv_area->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Date Add</td>
		<td<?php echo $adv->adv_dateadd->CellAttributes() ?>><span id="cb_x_adv_dateadd">
<input type="text" name="x_adv_dateadd" id="x_adv_dateadd"  value="<?php echo $adv->adv_dateadd->EditValue ?>"<?php echo $adv->adv_dateadd->EditAttributes() ?>>
&nbsp;<img src="images/ew_calendar.gif" id="cx_adv_dateadd" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_adv_dateadd", // ID of the input field
ifFormat : "%d/%m/%Y", // the date format
button : "cx_adv_dateadd" // ID of the button
}
);
</script>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Order</td>
		<td<?php echo $adv->adv_order->CellAttributes() ?>><span id="cb_x_adv_order">
<input type="text" name="x_adv_order" id="x_adv_order"  size="30" value="<?php echo $adv->adv_order->EditValue ?>"<?php echo $adv->adv_order->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Active</td>
		<td<?php echo $adv->adv_active->CellAttributes() ?>><span id="cb_x_adv_active">
<?php
$arwrk = $adv->adv_active->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($adv->adv_active->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_adv_active[]" id="x_adv_active[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $adv->adv_active->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="   Edit   ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Function Get upload files
function GetUploadFiles() {
	global $objForm, $adv;

	// Get upload data
		if ($adv->adv_img->Upload->UploadFile()) {

			// No action required
		} else {
			echo $adv->adv_img->Upload->Message;
			exit();
		}
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $adv;
	$adv->adv_id->setFormValue($objForm->GetValue("x_adv_id"));
	$adv->adv_title->setFormValue($objForm->GetValue("x_adv_title"));
	$adv->adv_link->setFormValue($objForm->GetValue("x_adv_link"));
	$adv->adv_area->setFormValue($objForm->GetValue("x_adv_area"));
	$adv->adv_dateadd->setFormValue($objForm->GetValue("x_adv_dateadd"));
	$adv->adv_dateadd->CurrentValue = ew_UnFormatDateTime($adv->adv_dateadd->CurrentValue, 7);
	$adv->adv_order->setFormValue($objForm->GetValue("x_adv_order"));
	$adv->adv_active->setFormValue($objForm->GetValue("x_adv_active"));
}

// Restore form values
function RestoreFormValues() {
	global $adv;
	$adv->adv_id->CurrentValue = $adv->adv_id->FormValue;
	$adv->adv_title->CurrentValue = $adv->adv_title->FormValue;
	$adv->adv_link->CurrentValue = $adv->adv_link->FormValue;
	$adv->adv_area->CurrentValue = $adv->adv_area->FormValue;
	$adv->adv_dateadd->CurrentValue = $adv->adv_dateadd->FormValue;
	$adv->adv_dateadd->CurrentValue = ew_UnFormatDateTime($adv->adv_dateadd->CurrentValue, 7);
	$adv->adv_order->CurrentValue = $adv->adv_order->FormValue;
	$adv->adv_active->CurrentValue = $adv->adv_active->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $adv;
	$sFilter = $adv->SqlKeyFilter();
	if (!is_numeric($adv->adv_id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@adv_id@", ew_AdjustSql($adv->adv_id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$adv->Row_Selecting($sFilter);

	// Load sql based on filter
	$adv->CurrentFilter = $sFilter;
	$sSql = $adv->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$adv->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $adv;
	$adv->adv_id->setDbValue($rs->fields('adv_id'));
	$adv->adv_title->setDbValue($rs->fields('adv_title'));
	$adv->adv_link->setDbValue($rs->fields('adv_link'));
	$adv->adv_img->Upload->DbValue = $rs->fields('adv_img');
	$adv->adv_area->setDbValue($rs->fields('adv_area'));
	$adv->adv_dateadd->setDbValue($rs->fields('adv_dateadd'));
	$adv->adv_order->setDbValue($rs->fields('adv_order'));
	$adv->adv_active->setDbValue($rs->fields('adv_active'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $adv;

	// Call Row Rendering event
	$adv->Row_Rendering();

	// Common render codes for all row types
	// adv_id

	$adv->adv_id->CellCssStyle = "";
	$adv->adv_id->CellCssClass = "";

	// adv_title
	$adv->adv_title->CellCssStyle = "";
	$adv->adv_title->CellCssClass = "";

	// adv_link
	$adv->adv_link->CellCssStyle = "";
	$adv->adv_link->CellCssClass = "";

	// adv_img
	$adv->adv_img->CellCssStyle = "";
	$adv->adv_img->CellCssClass = "";

	// adv_area
	$adv->adv_area->CellCssStyle = "";
	$adv->adv_area->CellCssClass = "";

	// adv_dateadd
	$adv->adv_dateadd->CellCssStyle = "";
	$adv->adv_dateadd->CellCssClass = "";

	// adv_order
	$adv->adv_order->CellCssStyle = "";
	$adv->adv_order->CellCssClass = "";

	// adv_active
	$adv->adv_active->CellCssStyle = "";
	$adv->adv_active->CellCssClass = "";
	if ($adv->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($adv->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($adv->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// adv_id
		$adv->adv_id->EditCustomAttributes = "";
		$adv->adv_id->EditValue = $adv->adv_id->CurrentValue;
		$adv->adv_id->CssStyle = "";
		$adv->adv_id->CssClass = "";
		$adv->adv_id->ViewCustomAttributes = "";

		// adv_title
		$adv->adv_title->EditCustomAttributes = "";
		$adv->adv_title->EditValue = ew_HtmlEncode($adv->adv_title->CurrentValue);

		// adv_link
		$adv->adv_link->EditCustomAttributes = "";
		$adv->adv_link->EditValue = ew_HtmlEncode($adv->adv_link->CurrentValue);

		// adv_img
		$adv->adv_img->EditCustomAttributes = "";
		$adv->adv_img->EditValue = $adv->adv_img->CurrentValue;

		// adv_area
		$adv->adv_area->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Left");
		$arwrk[] = array("2", "Center");
		$arwrk[] = array("3", "Right");
		$adv->adv_area->EditValue = $arwrk;

		// adv_dateadd
		$adv->adv_dateadd->EditCustomAttributes = "";
		$adv->adv_dateadd->EditValue = ew_HtmlEncode(ew_FormatDateTime($adv->adv_dateadd->CurrentValue, 7));

		// adv_order
		$adv->adv_order->EditCustomAttributes = "";
		$adv->adv_order->EditValue = ew_HtmlEncode($adv->adv_order->CurrentValue);

		// adv_active
		$adv->adv_active->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$adv->adv_active->EditValue = $arwrk;
	} elseif ($adv->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$adv->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $adv;
	$sFilter = $adv->SqlKeyFilter();
	if (!is_numeric($adv->adv_id->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@adv_id@", ew_AdjustSql($adv->adv_id->CurrentValue), $sFilter); // Replace key value
	$adv->CurrentFilter = $sFilter;
	$sSql = $adv->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field adv_id
		// Field adv_title

		$adv->adv_title->SetDbValueDef($adv->adv_title->CurrentValue, "");
		$rsnew['adv_title'] =& $adv->adv_title->DbValue;

		// Field adv_link
		$adv->adv_link->SetDbValueDef($adv->adv_link->CurrentValue, NULL);
		$rsnew['adv_link'] =& $adv->adv_link->DbValue;

		// Field adv_img
		$adv->adv_img->Upload->SaveToSession(); // Save file value to Session
		if ($adv->adv_img->Upload->Action == "2" || $adv->adv_img->Upload->Action == "3") { // Update/Remove
		$adv->adv_img->DbValue = $rs->fields('adv_img'); // Get original value
		if (is_null($adv->adv_img->Upload->Value)) {
			$adv->adv_img->Upload->DbValue = NULL;
		} else {
			$adv->adv_img->Upload->DbValue = ew_UploadFileNameEx(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $adv->adv_img->Upload->FileName);
		}
		$rsnew['adv_img'] =& $adv->adv_img->Upload->DbValue;
		}

		// Field adv_area
		$adv->adv_area->SetDbValueDef($adv->adv_area->CurrentValue, "");
		$rsnew['adv_area'] =& $adv->adv_area->DbValue;

		// Field adv_dateadd
		$adv->adv_dateadd->SetDbValueDef(ew_UnFormatDateTime($adv->adv_dateadd->CurrentValue, 7), NULL);
		$rsnew['adv_dateadd'] =& $adv->adv_dateadd->DbValue;

		// Field adv_order
		$adv->adv_order->SetDbValueDef($adv->adv_order->CurrentValue, NULL);
		$rsnew['adv_order'] =& $adv->adv_order->DbValue;

		// Field adv_active
		$adv->adv_active->SetDbValueDef($adv->adv_active->CurrentValue, NULL);
		$rsnew['adv_active'] =& $adv->adv_active->DbValue;

		// Call Row Updating event
		$bUpdateRow = $adv->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {

		// Field adv_img
			if (!is_null($adv->adv_img->Upload->Value)) ew_SaveFile(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $rsnew['adv_img'], $adv->adv_img->Upload->Value);
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($adv->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($adv->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $adv->CancelMessage;
				$adv->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$adv->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();

	// Field adv_img
	$adv->adv_img->Upload->RemoveFromSession(); // Remove file value from Session
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
